﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eXamineControls.SystemDescription.Draw.Item
{
    public partial class ComponentItem : UserControl, ISystemDescriptionItem
    {
        #region Variables
        private bool _selected;
        private Point _offset = Point.Empty;
        private Color _colorBack;
        private Cursor _cursor;

        #endregion

        #region Delegates and Event
        #endregion

        public ComponentItem()
        {
            InitializeComponent();
            this._selected = false;
        }


        #region Properties
        /// <summary>
        /// Gets or sets the name of the control.
        /// </summary>
        /// <returns>The name of the control. The default is an empty string ("").</returns>
        [Browsable(true)]
        public String Title
        {
            get { return this.name.Text; }
            set { this.name.Text = value; }
        }
        #endregion

        #region API
        #endregion

        #region Events
        private void SystemComponent_MouseHover(object sender, EventArgs e)
        {
            if (_offset == Point.Empty)
                this.BackColor = this.ColorMouseInside;
        }

        private void SystemComponent_MouseLeave(object sender, EventArgs e)
        {
            if (_offset == Point.Empty)
            {
                if (this._selected) this.BackColor = this.ColorSelected;
                else this.BackColor = this.ColorBackground;
            }
        }
        private void SystemComponent_MouseEnter(object sender, EventArgs e)
        {
            if (_offset == Point.Empty)
                this.BackColor = this.ColorMouseInside;
        }

        private void ComponentItem_MouseClick(object sender, MouseEventArgs e)
        {
            this.OnItemSelected();
        }
        private void SystemComponent_MouseDown(object sender, MouseEventArgs e)
        {
            _offset = new Point(e.X, e.Y);
        }
        private void SystemComponent_MouseUp(object sender, MouseEventArgs e)
        {
            _offset = Point.Empty;
            if (this._selected) this.BackColor = this.ColorSelected;
            else if (!e.Location.IsEmpty) this.BackColor = this.ColorMouseInside;
            else this.BackColor = this.ColorBackground;
        }
        private void SystemComponent_MouseMove(object sender, MouseEventArgs e)
        {
            if (this._offset != Point.Empty)
            {
                Point newlocation = this.Location;
                newlocation.X += e.X - _offset.X;
                newlocation.Y += e.Y - _offset.Y;
                if (newlocation.X < 0) newlocation.X = 0;
                if (newlocation.Y < 0) newlocation.Y = 0;
                this.Location = newlocation;
                this.BackColor = this.ColorDrag;
            } else
                this.BackColor = this.ColorMouseInside;
        }
        protected virtual void OnItemSelected()
        {
            if (ItemSelected != null)
            {
                ItemSelected(this, new EventArgs());
            }
        }
        #endregion

        #region DrawObject
        [Browsable(true)]
        public Color ColorBackground
        {
            get { return _colorBack; }
            set
            {
                this.BackColor = value;
                _colorBack = value;
            }
        }

        [Browsable(true)]
        public Color ColorMouseInside { get; set; }

        [Browsable(true)]
        public Color ColorSelected { get; set; }

        [Browsable(true)]
        public Color ColorDrag { get; set; }

        public void SelectItem()
        {
            this._selected = true;
            this.BackColor = this.ColorSelected;
        }
        public void DeselectItem()
        {
            this._selected = false;
            this.BackColor = this.ColorBackground;
        }

        public void BeginConnectionMode()
        {
            this._cursor = this.Cursor;
            this.Cursor = Cursors.Hand;
            this.titlePanel.Cursor = Cursors.Hand;
            this.name.Cursor = Cursors.Hand;
        }

        public void EndConnectionMode()
        {
            this.Cursor = this._cursor;
            this.titlePanel.Cursor = this._cursor;
            this.name.Cursor = this._cursor;
        }
        public event EventHandler ItemSelected;
        #endregion
    }
}
